package com.sy.ecommerce.stream.sy;

import com.alibaba.fastjson.JSON;
import com.sy.ecommerce.vo.SyMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;

/**
 * <h1>使用自定义的通信信道 SySource 实现消息的发送</h1>
 * */
@Slf4j
@EnableBinding(SySource.class)
public class SySendService {

    private final SySource sySource;

    public SySendService(SySource sySource) {
        this.sySource = sySource;
    }

    /**
     * <h2>使用自定义的输出信道发送消息</h2>
     * */
    public void sendMessage(SyMessage message) {

        String _message = JSON.toJSONString(message);
        log.info("in SySendService send message: [{}]", _message);
        sySource.syOutput().send(
                MessageBuilder.withPayload(_message).build()
        );
    }
}
